import java.io.{File, PrintWriter}
import java.util.Date

/**
  * @Description:
  * @author caoyangjie
  * @date 2020年01月2020/1/4日 17:54
  * @Copyright: © 赢时胜
  */
class Rational(n:Int,d:Int) {
  require(d!=0)
  private def g = gcd(n,d)
  val number = n/g
  val damon = d/g
  override def toString: String = n+"/"+d
  def +(that:Rational):Rational={
    new Rational(this.n*that.damon+that.number*this.d,this.d*that.damon)
  }
  def gcd(n:Int,d:Int):Int={
    if(d==0)
      n
    else
      gcd(d,n%d)
  }
}
object Rational extends App{
  implicit def intToR(value:Int)=new Rational(value,1)

  val r1 = new Rational(3,8)
  val r2 = new Rational(7,11)
  println(r1)
  println(r2)
  println(r1 + r2)
  println(2 + r1)
  println(r1 + 2)
  private def filesHere = (new File("E:\\platform\\github\\spring-stack-skills\\skill-scala\\src")).listFiles();

  private def filesMatching(matcher:String=>Boolean) =
    for (file<- filesHere; if matcher(file.getName))
      yield file

  def filesEnding(query:String) = filesMatching(_.endsWith(query))
  def filesContaining(query:String) = filesMatching(_.contains(query))
  def fileRegex(query:String) = filesMatching(_.matches(query))

  def withPrintWriter(file:File)(op:PrintWriter=>Unit): Unit = {
    val writer = new PrintWriter(file)
    try{
      op(writer)
    }finally {
      writer.close();
    }
  }
  filesEnding(".scala").foreach(println _)
  filesContaining("Tes").foreach(println _)

  withPrintWriter(new File("E:\\platform\\github\\spring-stack-skills\\skill-scala\\src\\ScalaTest.txt")){
    writer=>writer.println(new Date())
  }
  val assertionEnabled = true;
  def Assert(predicate: =>Boolean)(errorMsg:String) =
    if( assertionEnabled && !predicate ){
      throw new AssertionError(errorMsg)
    }

  Assert(5>3)("这是成立的")
  Assert(5<3)("这是不成立的")
}
